package com.atguigu.lease.admin.controller.apartment;

import com.atguigu.lease.admin.service.ApartmentInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.ApartmentInfo;
import com.atguigu.lease.model.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.model.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.model.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.model.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "公寓管理模块")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {

    @Autowired
    ApartmentInfoService apartmentInfoService;


    /*
        1、创建接口接受请求
        2、分析如何接受请求参数
        3、处理业务
            业务判断处理
            数据库表处理
        4、分析如何响应数据给前端
     */
    @Operation(summary = "1,新增或者更新公寓")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {
        apartmentInfoService.saveOrUpdateApartmentSubmitVo(apartmentSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "2,分页展示公寓")
    @GetMapping("/pageItem")
    public Result<Page<ApartmentItemVo>> pageItem(@RequestParam Long current
            , @RequestParam Long size
            , ApartmentQueryVo queryVo) {
        Page<ApartmentItemVo> page = apartmentInfoService.getPageItem(current, size, queryVo);
        return Result.ok(page);
    }

    @Operation(summary = "3,根据ID查询公寓详情")
    @GetMapping("/getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {
        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.getDetailById(id);
        return Result.ok(apartmentDetailVo);
    }

    @Operation(summary = "4,删除公寓")
    @DeleteMapping("/removeById")
    public Result removeById(@RequestParam Long id) {
        apartmentInfoService.deleteApartmentById(id);
        return Result.ok();
    }

    @Operation(summary = "5、更新公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id ,
                                          @RequestParam String status) {
        apartmentInfoService.update(Wrappers.lambdaUpdate(ApartmentInfo.class)
                .eq(ApartmentInfo::getId, id)
                .set(ApartmentInfo::getIsRelease , status));
        return Result.ok();
    }

    @Operation(summary = "6、根据区县id查询公寓列表")
    @GetMapping("/listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        //sql条件的构建器
        LambdaQueryWrapper<ApartmentInfo> queryWrapper =
                new LambdaQueryWrapper<>(ApartmentInfo.class);
        // select * from apartment_info where district_id = #{id}
        queryWrapper.eq(ApartmentInfo::getDistrictId, id);
        List<ApartmentInfo> apartmentInfos = apartmentInfoService.list(queryWrapper);
        return Result.ok(apartmentInfos);
    }
}